package com.colin.app.fragment.home;


import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.app.app.AppFragment;
import com.colin.library.help.InitViewHelp;
import com.colin.library.help.LogHelp;
import com.colin.library.listener.IWebViewCallBack;
import com.colin.library.other.Url;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends AppFragment implements HomeContract.HomeView {
    private HomeContract.HomePresenter mHomePresenter = null;
    private static final String FRAGMENT_ID = "fragment_id";
    private int fragment_id;
    private WebView webview_content;
    private ProgressBar progress_bar_webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        fragment_id = bundle.getInt(FRAGMENT_ID);
    }
    @Override
    public void onDestroyView() {
        if (webview_content != null) {
            webview_content.removeAllViews();
            try {
                webview_content.destroy();
            } catch (Throwable t) {
            }
            webview_content = null;
        }
        if (null!=mHomePresenter){
            mHomePresenter.destroy();
            mHomePresenter = null;
        }
        super.onDestroyView();
    }

    public HomeFragment() {

    }

    public static HomeFragment newInstance(AppActivity mAppActivity, int fragment_id) {
        activity = mAppActivity;
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(FRAGMENT_ID, fragment_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initPresenter() {
        mHomePresenter = new HomePresenterImpl((AppActivity) activity, this);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        this.webview_content = (WebView) this.getView().findViewById(R.id.webview_content);
        this.progress_bar_webview = (ProgressBar) this.getView().findViewById(R.id.progress_bar_webview);
        InitViewHelp.getInstance().initWebView(webview_content, Url.BAIDU_URL, new IWebViewCallBack() {

            @Override
            public void loadUrl(String url) {
                LogHelp.d(url);
            }

            @Override
            public void setTitle(String title) {
                LogHelp.d(title);
            }

            @Override
            public void loadProgress(int gone,int progress) {
                LogHelp.d("progress=="+String.valueOf(progress));
                progress_bar_webview.setProgress(progress);
                progress_bar_webview.setVisibility(gone);
            }

            @Override
            public void error(int errorCode, String description, String failingUrl) {
                LogHelp.e(description);
                LogHelp.e(failingUrl);
                toastShow(description);
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }



    private boolean webviewBack() {
        if (webview_content.canGoBack()) {
            webview_content.goBack();
            return false;
        } else {
            return true;
        }
    }
}
